package net.airtheva.uniqueTask4.view;

import net.airtheva.uniqueTask4.LyricParser;
import net.airtheva.uniqueTask4.Utils;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.TextView;

public class FloatLyricView extends TextView {

	public static final String TAG = "FloatLyricView";

	boolean mIsShowed = false;
	
	WindowManager.LayoutParams mLayoutParams;
	
	float mPreviousX = 0;
	float mPreviousY = 0;
	
	Long[] mTimes;
	String[] mLines;

	boolean mIsLoaded = false;

	public FloatLyricView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub

		setGravity(Gravity.CENTER);
		setText(TAG);

	}

	public void SetLyricParser(LyricParser lyricParser) {

		mTimes = lyricParser.GetTimes();
		mLines = lyricParser.GetLines();

		mIsLoaded = true;

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_MOVE:
			updatePosition(event.getRawX() - mPreviousX, event.getRawY() - mPreviousY);
			break;
		case MotionEvent.ACTION_UP:
			break;
		}

		mPreviousX = event.getRawX();
		mPreviousY = event.getRawY();
		
		return super.onTouchEvent(event);
	}
	
	protected void updatePosition(float deltaX, float deltaY) {
		
		mLayoutParams.x += deltaX;
		mLayoutParams.y += deltaY;
		
		((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).updateViewLayout(this, mLayoutParams);
		
	}

	// Use to change the current line, and automatically scroll to it.
	protected void setCurrentIndex(int currentIndex) {

		if (mIsLoaded) {

			setText(mLines[currentIndex]);

		}

	}

	public boolean GetIsShowed() {
		
		return mIsShowed;
		
	}
	
	
	public void Show() {
		
		if(!mIsShowed) {
			
			WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
			
			Display d = wm.getDefaultDisplay();
			Point size = new Point();
			d.getSize(size);
			
			mLayoutParams = new WindowManager.LayoutParams();
			
			mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
			mLayoutParams.format = PixelFormat.RGBA_8888;
			
			mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
			
			mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
			mLayoutParams.x = 0;
			mLayoutParams.y = size.y - getMeasuredHeight();
			
			//mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
			mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
			mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
			
			wm.addView(this, mLayoutParams);
			
			mIsShowed = true;
			
		}
		
	}
	
	public void SetTime(long time) {

		if (mIsLoaded) {

			int index = Utils.FindArea(mTimes, time);

			if (index >= 0) {
				setCurrentIndex(index);
			} else {
				Log.d(TAG, "FindArea == -1.");
			}

		}

	}
	
	public void Hide() {
		
		if(mIsShowed) {
			
			WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
			
			wm.removeView(this);
			
			mIsShowed = false;
			
		}
		
	}

}
